package com.giscloud.limp.service.impl;

import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.JacksonUtil;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.CaseCloseDao;
import com.giscloud.limp.dao.CaseFirstInstanceDao;
import com.giscloud.limp.entity.*;
import com.giscloud.limp.entity.dto.CaseCloseDTO;
import com.giscloud.limp.service.CaseCloseService;
import com.giscloud.limp.service.CaseWarnService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("caseCloseService")
@Log4j2
public class CaseCloseServiceImpl implements CaseCloseService {
    private final  CaseCloseDao caseCloseDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    private CaseFirstInstanceDao caseFirstInstanceDao;

    @Autowired
    private CaseWarnService caseWarnService;

    @Autowired
    public CaseCloseServiceImpl(CaseCloseDao caseCloseDao) {
        this.caseCloseDao = caseCloseDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseCloseDTO caseCloseDTO) {
        log.debug("#queryPage parameter ={}", JacksonUtil.toJSONString(caseCloseDTO));
        PageHelper.startPage(caseCloseDTO.getPage(), caseCloseDTO.getRows());
        CaseCloseExample caseCloseExample = new CaseCloseExample();
        CaseCloseExample.Criteria criteria = caseCloseExample.createCriteria();
        List<CaseClose> caseCloses = caseCloseDao.selectByExample(caseCloseExample);
        return PageInfo.of(caseCloses);
    }


    /**
     * 信息
     */
    @Override
    public CaseClose getById(Integer caseCloseId){
        log.debug("#getById parameter ={}", caseCloseId);
        return caseCloseDao.selectByPrimaryKey(caseCloseId);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(CaseClose caseClose){
        log.debug("#save parameter ={}", JacksonUtil.toJSONString(caseClose));
        caseClose.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        if(caseCloseDao.insertSelective(caseClose) > 0 ){
            CaseWarn caseWarn = new CaseWarn();
            caseWarn.setCaseId(caseClose.getCaseId());
            caseWarn.setLitigationStage(5);
            caseWarn.setWarnDate(caseClose.getWarnDate());
            caseWarn.setWarnContent(caseClose.getWarnContent());
            caseWarnService.save(caseWarn);
          //  CaseFirstInstance queryVO = caseFirstInstanceDao.selectByPrimaryKey(caseClose.getCaseId());
            CaseFirstInstance caseFirstInstance = new CaseFirstInstance();
            caseFirstInstance.setClosecaseType(caseClose.getClosecaseType());
            caseFirstInstance.setClosecaseStage(caseClose.getClosecaseStage());
            if(caseClose.getClosecaseStage() != null && caseClose.getClosecaseStage() == 1){
                caseFirstInstance.setCaseStatus(1);
                caseFirstInstance.setLitigationStage(-1);
            }else{
                caseFirstInstance.setLitigationStage(5);
            }
            CaseFirstInstanceExample example = new CaseFirstInstanceExample();
            example.createCriteria().andCaseIdEqualTo(caseClose.getCaseId());
            caseFirstInstanceDao.updateByExampleSelective(caseFirstInstance,example);
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(CaseClose caseClose){
         log.debug("#updateById parameter ={}", JacksonUtil.toJSONString(caseClose));
         caseClose.setOperateStaffId(tokenUserUtil.getUserId().intValue());
         caseClose.setVersion(new Date());
         CaseCloseExample caseCloseExample = new CaseCloseExample();
         CaseCloseExample.Criteria criteria = caseCloseExample.createCriteria();
         criteria.andCaseCloseIdEqualTo(caseClose.getCaseCloseId());
         if(caseCloseDao.updateByExampleSelective(caseClose,caseCloseExample) > 0){
             CaseWarn caseWarn = new CaseWarn();
             caseWarn.setCaseId(caseClose.getCaseId());
             caseWarn.setLitigationStage(5);
             caseWarn.setWarnDate(caseClose.getWarnDate());
             caseWarn.setWarnContent(caseClose.getWarnContent());
             caseWarnService.updateById(caseWarn);
             CaseFirstInstance caseFirstInstance = new CaseFirstInstance();
             caseFirstInstance.setClosecaseType(caseClose.getClosecaseType());
             caseFirstInstance.setClosecaseStage(caseClose.getClosecaseStage());
             if(caseClose.getClosecaseStage() != null && caseClose.getClosecaseStage() == 1){
                 caseFirstInstance.setCaseStatus(1);
                 caseFirstInstance.setLitigationStage(-1);
             }else{
                 caseFirstInstance.setLitigationStage(5);
             }
             CaseFirstInstanceExample example = new CaseFirstInstanceExample();
             example.createCriteria().andCaseIdEqualTo(caseClose.getCaseId());
             caseFirstInstanceDao.updateByExampleSelective(caseFirstInstance,example);
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> caseCloseIds){
        log.debug("#removeByIds parameter ={}", JacksonUtil.toJSONString(caseCloseIds));
        CaseCloseExample caseCloseExample = new CaseCloseExample();
        CaseCloseExample.Criteria criteria = caseCloseExample.createCriteria();
        criteria.andCaseCloseIdIn(caseCloseIds);
        List<CaseClose> caseCloses = caseCloseDao.selectByExample(caseCloseExample);
        if(CollectionUtils.isNotEmpty(caseCloses)){
            caseCloseDao.deleteByExample(caseCloseExample);
            for(CaseClose caseClose : caseCloses){
                CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseClose.getCaseId());
                caseFirstInstance.setCaseId(caseClose.getCaseId());
                caseFirstInstanceDao.updateCaseClose(caseFirstInstance);
                CaseWarn caseWarn = new CaseWarn();
                caseWarn.setCaseId(caseClose.getCaseId());
                caseWarn.setLitigationStage(5);
                caseWarnService.remove(caseWarn);
            }

        }
        return Result.success("删除成功");
    }

}